home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / comm / bbs / Hydra11s.lha / HBBS / Source / Utils / AX2HBBS / AX2HBBS.c < prev    next >
C/C++ Source or Header  |  1996-06-25  |  8KB  |  358 lines

  1. /*
  2.  
  3. Program     :  AmiExpress FileList to HBBS Filelist convertor.
  4.  
  5. Filename    :  AX2HBBS
  6.  
  7. Usage       :  AX2HBBS infile outfile [Year Prefix(2 characters only)]
  8.  
  9. Author      :  Ben Clifton
  10.  
  11. Environment :  Amiga
  12.  
  13. Revision    :  V1.0 17 Dec 1995
  14.  
  15. */
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. #include <clib/dos_protos.h>
  22.  
  23. #define BUFLEN 256
  24.  
  25. /* GLOBAL Variables */
  26. BOOL First = TRUE;
  27. char DateYearPrefix[5];
  28.  
  29. /* 12, 22, 32 */
  30.  
  31. /* haha, bit of code taken from FileMan by Ben Clifton (Oh, thats me!, cool).
  32.  *
  33.  *
  34.  * FM-strFNcpy - copy "num" characters from "start" position in "in" to "out"
  35.  *              and NULL terminate.
  36.  *
  37.  * Note : out is NOT allocated.
  38.  */
  39.  
  40. void FM_strFNcpy( char *in, char *out, int start, int num )
  41. {
  42.   int loop, i;
  43.  
  44.   for( i=0, loop = start; loop < num+start; loop++, i++ )
  45.   {
  46.     out[i] = in[loop];
  47.   }
  48.   out[i]=0;
  49. }
  50.  
  51. /* return a Month string in format "MMM-"
  52.  *
  53.  * the "-" is there cos I`m a lazy bastard!
  54.  */
  55.  
  56. char *GiveMonth( int num )
  57. {
  58.   switch( num )
  59.   {
  60.     case 1:
  61.       return( "JAN-" );
  62.     case 2:
  63.       return( "FEB-" );
  64.     case 3:
  65.       return( "MAR-" );
  66.     case 4:
  67.       return( "APR-" );
  68.     case 5:
  69.       return( "MAY-" );
  70.     case 6:
  71.       return( "JUN-" );
  72.     case 7:
  73.       return( "JUL-" );
  74.     case 8:
  75.       return( "AUG-" );
  76.     case 9:
  77.       return( "SEP-" );
  78.     case 10:
  79.       return( "OCT-" );
  80.     case 11:
  81.       return( "NOV-" );
  82.     case 12:
  83.       return( "DEC-" );
  84.     default:
  85.       return( "Erk-" );
  86.   }
  87. }
  88.  
  89. /* You`ll never guess what this does!  , oh shit you have.... never mind.
  90.  *
  91.  * Um, if anyone is dumb enough not to know what this does then:
  92.  *
  93.  * Convert Line - Converts a string, in, in /X format into, out, in HBBS
  94.  *               format.
  95.  *
  96.  * Note : out is written TO not allocated.
  97.  *
  98.  */
  99.  
  100. BOOL ConvertLine( char *in, char *out )
  101. {
  102.   char temp[BUFLEN], temp2[BUFLEN];
  103.   char *ptr;
  104.   int i, length;
  105.   long filesize;
  106.  
  107.   out[0] = 0;
  108.   if( in[0] != ' ' )
  109.   {
  110.     /* This is to put a blank line separator between FileIds */
  111.     if( !First )
  112.     {
  113.       strcat( out, "\n" );
  114.     }
  115.     else
  116.     {
  117.       First = FALSE;
  118.     }
  119.  
  120.     strcat( out, "F " );
  121.  
  122.     /* Concat FileName top output string */
  123.     FM_strFNcpy( in, temp, 0, 12 );  /* Its the first 12 bytes so get them */
  124.     strcat( out, temp );
  125.     strcat( out, " " );  /* Separator */
  126.  
  127.     /* Do filesize */
  128.     FM_strFNcpy( in, temp, 14, 7 );
  129.     length = strlen( temp );
  130.     i=-1;
  131.     do
  132.     {
  133.       i++;
  134.       ptr = &temp[i];
  135.     }while( !( (temp[i]>='0') && (temp[i]<='9') ) && i<9 );
  136.     filesize = atol( temp );
  137.     sprintf( temp, "%8d ", filesize );
  138.     strcat( out, temp );
  139.  
  140.     /* Do Date! */
  141.  
  142.     /* before we do anything, I`ll pointlessly get a copy of the needed
  143.      * bit of data!  - okay so its not pointless, it lets me change the
  144.      * position easily! */
  145.  
  146.     FM_strFNcpy( in, temp, 23, 8 );
  147.  
  148.     /* First we have the Day...*/
  149.  
  150.     FM_strFNcpy( temp, temp2, 3, 2 );
  151.     strcat( out, temp2 );
  152.     strcat( out, "-" );
  153.  
  154.     /* Second we have the month, curtosy of a simple case statement! :) */
  155.  
  156.     FM_strFNcpy( temp, temp2, 0, 2 );
  157.     strcat( out, GiveMonth( atoi(temp2) ) );  /* wow, i love this stuff, it SOOOOOOo cool! <smirk> */
  158.  
  159.     /* and finally we have the YEAR!  god, is it that already?  where did my life go:-)))) */
  160.  
  161.     FM_strFNcpy( temp, temp2, 6, 2 );
  162.     strcat( out, DateYearPrefix );
  163.     strcat( out, temp2 );
  164.     strcat( out, " " );
  165.  
  166.     /* And after all this we get to put the file id line down... */
  167.     ptr = &in[33];
  168.     strcat( out, ptr );
  169.   }
  170.   else
  171.   {
  172.     /*if its not a "F" then its a "I". */
  173.     strcat( out, "I" );
  174.     /* Pad with spaces */
  175.     for( i=0; i<35; i++)
  176.     {
  177.       strcat( out, " " );
  178.     }
  179.     /* :) */  /* I LOVE this method, its SO cheeky! */
  180.     ptr = &in[33];
  181.     strcat( out, ptr );
  182.   }
  183.  
  184.   return( TRUE );
  185. }
  186.  
  187. /* Check string, s, to make sure its not garbage. */
  188.  
  189. BOOL CheckLineForGarbage( char *s )
  190. {
  191.   if( (s[12] == ' ')&&(s[22] == ' ')&&(s[32] == ' ') )
  192.     return( FALSE );
  193.   else
  194.     return( TRUE );
  195. }
  196.  
  197. /* Main program : To convert an AmiExpress FileList to an HBBS FileList
  198.  *
  199.  * Inputs   :  Input FileName, OutputFileName, Year Prefix
  200.  *
  201.  * Outputs  :  New HBBS format Filelist.
  202.  *
  203.  *
  204.  * Usage    : AX2HBBS infile outfile [Year Prefix(2 characters only)]
  205.  *
  206.  * Example 1: AX2HBBS infile outfile
  207.  * Example 2: AX2HBBS infile outfile 19
  208.  *
  209.  *
  210.  * Notes    : if outfile exists then AX2HBBS will append the data.
  211.  *
  212.  */
  213.  
  214. void main( int argc, char *argv[] )
  215. {
  216.   BPTR infile, outfile;
  217.   BOOL done = FALSE, error = FALSE;
  218.   char inbuffer[BUFLEN], outbuffer[BUFLEN];
  219.   long openmode;
  220.  
  221.   if( (argc != 3)&&(argc != 4) )
  222.   {
  223.     Printf( "AX2HBBS by Ben Clifton.\nPurpose : To convert /X FileLists to HBBS FileLists\n");
  224.     Printf( "AX2HBBS infile outfile [Year Prefix(2 characters only)]\n" );
  225.   }
  226.   else
  227.   {
  228.     if( argc == 3 )
  229.     {
  230.       strcpy( DateYearPrefix, "19" );
  231.     }
  232.     else
  233.     {
  234.       /* Make sure we get 2 characters and ONLY 2 characters */
  235.       if( strlen( argv[3]) >= 2 )
  236.       {
  237.         FM_strFNcpy( argv[3], DateYearPrefix, 0, 2 );
  238.       }
  239.       else
  240.       {
  241.         /* don`t know why really...  cos it SHOULD be at least 1995... oh well. */
  242.         DateYearPrefix[0] = '0';
  243.         DateYearPrefix[1] = argv[3][0];
  244.         DateYearPrefix[2] = 0;
  245.       }
  246.     }
  247.     if( !(infile = Open( argv[1], MODE_OLDFILE )))
  248.     {
  249.       Printf( "Error : Unable to find input file\n" );
  250.     }
  251.     else
  252.     {
  253.       Printf( "Skipping Garbage......." );
  254.       while( !done )
  255.       {
  256.         if( !(FGets( infile, inbuffer, BUFLEN-1 )))
  257.         {
  258.           done = TRUE;
  259.           error = TRUE;
  260.           Printf( "Error : Cannot read from infile\n" );
  261.         }
  262.         else
  263.         {
  264.           if( !(CheckLineForGarbage( inbuffer )))
  265.           {
  266.             done = TRUE;
  267.           }
  268.         }
  269.       }
  270.       if( !error )
  271.       {
  272.         Printf( "Done.     No errors.\n" );
  273.  
  274.         if( outfile = Lock( argv[2], SHARED_LOCK ) )
  275.         {
  276.           First = FALSE;
  277.           openmode = MODE_OLDFILE;
  278.           UnLock( outfile );
  279.         }
  280.         else
  281.         {
  282.           openmode = MODE_NEWFILE;
  283.           First = TRUE;
  284.         }
  285.  
  286.         if( !(outfile = Open( argv[2], openmode )))
  287.         {
  288.           Printf( "Error : Unable to open outfile.\n" );
  289.           error = TRUE;
  290.         }
  291.         else
  292.         {
  293.           Printf( "Processing............." );
  294.           Seek( outfile, 0, OFFSET_END );
  295.           /* Write out header! */
  296.           if( !(FPuts( outfile,     "\n    +------------------------------------------------+\n" )))            {
  297.             if( !(FPuts( outfile,     "    |   FileList created by AX2HBBS by Ben Clifton   |\n" )))              {
  298.               if( !(FPuts( outfile,   "    | Another cool util from Ruby Knight Productions |\n" )))                {
  299.                 if( !(FPuts( outfile, "    +------------------------------------------------+\n" )))
  300.                 {
  301.                   done = FALSE;
  302.                   while( !done )
  303.                   {
  304.                     if( ConvertLine( inbuffer, outbuffer ) )
  305.                     {
  306.                       if( FPuts( outfile, outbuffer ))
  307.                       {
  308.                         done = TRUE;
  309.                         error = TRUE;
  310.                         Printf( "Error: writing to outfile\n" );
  311.                       }
  312.                       else
  313.                       {
  314.                         if( !(FGets( infile, inbuffer, BUFLEN-1 )))
  315.                         {
  316.                           done = TRUE;
  317.                         }
  318.                       }
  319.                     }
  320.                   }
  321.                 }
  322.                 else
  323.                 {
  324.                   error = TRUE;
  325.                 }
  326.               }
  327.               else
  328.               {
  329.                 error = TRUE;
  330.               }
  331.             }
  332.             else
  333.             {
  334.               error = TRUE;
  335.             }
  336.           }
  337.           else
  338.           {
  339.             error = TRUE;
  340.           }
  341.           Close( outfile );
  342.         }
  343.         if( !error )
  344.         {
  345.           Printf( "Finished. No errors.\n" );
  346.         }
  347.         else
  348.         {
  349.           Printf( "Finished. Error has occured!\n" );
  350.         }
  351.       }
  352.       Close( infile );
  353.     }
  354.   }
  355. }
  356.  
  357. /* Bananas are cool!  - just for Dom! */
  358.